package com.tacc.tyun.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tacc.tyun.base.BaseConstant;
import com.tacc.tyun.base.BaseServiceImpl;
import com.tacc.tyun.dao.master.BaseMenuDao;
import com.tacc.tyun.model.BaseMenu;
import com.tacc.tyun.service.BaseMenuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Yu Shengqiang on 2017/5/3.
 */
@Service("baseMenuService")
public class  BaseMenuServiceImpl extends BaseServiceImpl<BaseMenu> implements BaseMenuService {
    @Resource
    private BaseMenuDao baseMenuDao;
    @Autowired
    public void setBaseMenuDao(BaseMenuDao baseMenuDao) {
        this.baseDao = baseMenuDao;
    }
    //分页查询
    public PageInfo<BaseMenu> pageInfo(BaseMenu baseMenu, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy("menunum desc");
        //通用Example查询
        Example example = new Example(BaseMenu.class);
        if(StringUtils.isNotEmpty(baseMenu.getMenuname())){
            example.createCriteria().andLike("menuname","%"+baseMenu.getMenuname()+"%");
        }
        if(StringUtils.isNotEmpty(baseMenu.getRemovetag())){
            example.createCriteria().andEqualTo("removetag",baseMenu.getRemovetag());
        }
        List<BaseMenu> list=baseMenuDao.selectByExample(example);
        return new PageInfo<BaseMenu>(list);
    }

    //获取最大menuCode值
    public String AcceptMaxMenuCode(BaseMenu baseMenu){
        return baseMenuDao.AcceptMaxMenuCode(baseMenu);
    }
    //获取最大menuCode值
    public String AcceptMaxMenuCode(String menuCode){
        BaseMenu baseMenu=new BaseMenu();
        baseMenu.setMenucode(menuCode);
        baseMenu.setRemovetag(BaseConstant.REMOVE_N);
        String menuCodeMax =baseMenuDao.AcceptMaxMenuCode(baseMenu);
        if(StringUtils.isEmpty(menuCodeMax)){
            menuCodeMax=BaseConstant.MENU_CODE_MIN;
        }else{
            menuCodeMax=Integer.parseInt(menuCodeMax)+1+"";
        }
        return menuCodeMax;
    }
    //获取最大menuCode值
    public String AcceptMaxMenuCode(String parentId,String menuGrade){
        BaseMenu baseMenu=new BaseMenu();
        baseMenu.setParentmenuid(parentId);
        baseMenu.setMenugrade(menuGrade);
        baseMenu.setRemovetag(BaseConstant.REMOVE_N);
        String menuCodeMax =baseMenuDao.AcceptMaxMenuCode(baseMenu);
        if(StringUtils.isEmpty(menuCodeMax)){
            menuCodeMax=BaseConstant.MENU_CODE_MIN;
        }else{
            menuCodeMax=Integer.parseInt(menuCodeMax)+1+"";
        }
        return menuCodeMax;
    }
}
